package com.ttg.common.sftp;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.ftp.Ftp;
import com.ttg.common.enums.ErrorCodeEnums;
import com.ttg.common.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;


@Slf4j
public class FtpUtils {

    public static void download(FtpProperties config, String remotePath, String remoteFilename, String localPath, String localFileName){
        if ( StringUtils.isNotBlank(remotePath) && (remotePath.contains(" ")||remotePath.contains("..")||remotePath.contains("./")||remotePath.contains("/."))){
            return;
        }
        if ( StringUtils.isNotBlank(remoteFilename) && (remoteFilename.contains(" ")||remoteFilename.contains("..")||remoteFilename.contains("./")||remoteFilename.contains("/."))){
            return;
        }
        if (FtpTypeEnum.FTP.getCode().equals(config.getType())) {
            Ftp ftp = null;
            try {
                ftp = new Ftp(config.getServerIp(),config.getPort(),config.getUsername(),config.getPassword());
                if (ftp.existFile(remotePath + remoteFilename)) {
                    File file = new File(localPath);
                    if (!file.exists()){
                        if (!file.mkdirs()) {
                            log.error(localPath + localFileName + "路径创建失败");
                            throw new BaseException(ErrorCodeEnums.BANK_FILE_PATH_NOT_EXISTS);
                        }

                    }
                    ftp.download(remotePath, remoteFilename, FileUtil.file(localPath + localFileName));
                } else {
                    log.error(remotePath + remoteFilename + "文件不存在");
                    throw new RuntimeException("ftp文件不存在");
                }
            } catch (Exception e) {
                log.error("异常:{}",e.getMessage());
                throw e;
            }finally {
                if (ftp != null) {
                    try {
                        ftp.close();
                    } catch (Exception e) {
                        log.error("异常{}",e.getMessage());
                    }
                }
            }
        }else {
            try {
                SftpConfig sftpConfig = new SftpConfig(config.getServerIp(),config.getPort(),config.getUsername(),config.getPassword());
                sftpConfig.setExecType(SftpExecTypeEnum.DOWNLOAD);
                sftpConfig.setRemotePath(remotePath);
                sftpConfig.setRemoteFilename(remoteFilename);
                sftpConfig.setLocalPath(localPath);
                sftpConfig.setLocalFilename(localFileName);
                sftpConfig.setPrivateKey(config.getPrivateKey());
                sftpConfig.setProxyHost(config.getProxyHost());
                sftpConfig.setProxyPort(config.getProxyPort());
                sftpConfig.setProxyType(ProxyTypeEnum.getByCode(config.getProxyType()));
                SftpExecutor.execute(sftpConfig);
            } catch (Exception e) {
                log.error("下载文件失败：{}",e.getMessage());
                throw new RuntimeException("sftp 下载文件失败:"+e.getMessage());
            }
        }
    }

    public static void upload(FtpProperties config, String remotePath, String remoteFilename, String localPath, String localFileName){
        if (FtpTypeEnum.FTP.getCode().equals(config.getType())) {
            Ftp ftp = null;
            try {
                ftp = new Ftp(config.getServerIp(),config.getPort(),config.getUsername(),config.getPassword());
                if (ftp.existFile(localPath + localFileName)) {
                    ftp.upload(remotePath, remoteFilename, FileUtil.file(localPath + localFileName));
                } else {
                    log.error(localPath + localFileName+ "文件不存在");
                    throw new RuntimeException("ftp文件不存在");
                }
            } catch (Exception e) {
                log.error("异常:{}",e.getMessage());
                throw e;
            }finally {
                if (ftp != null) {
                    try {
                        ftp.close();
                    } catch (Exception e) {
                        log.error("异常{}",e.getMessage());
                    }
                }
            }
        } else {
            try {
                SftpConfig sftpConfig = new SftpConfig(config.getServerIp(),config.getPort(),config.getUsername(),config.getPassword());
                sftpConfig.setExecType(SftpExecTypeEnum.UPLOAD);
                sftpConfig.setRemotePath(remotePath);
                sftpConfig.setRemoteFilename(remoteFilename);
                sftpConfig.setLocalPath(localPath);
                sftpConfig.setLocalFilename(localFileName);
                sftpConfig.setPrivateKey(config.getPrivateKey());
                sftpConfig.setProxyHost(config.getProxyHost());
                sftpConfig.setProxyPort(config.getProxyPort());
                sftpConfig.setProxyType(ProxyTypeEnum.getByCode(config.getProxyType()));
                SftpExecutor.execute(sftpConfig);
            } catch (Exception e) {
                log.error("下载文件失败：{}",e.getMessage());
                throw new RuntimeException("sftp 下载文件失败:"+e.getMessage());
            }
        }
    }
    public static void delete(FtpProperties config, String remotePath, String remoteFilename){
        if (FtpTypeEnum.FTP.getCode().equals(config.getType())) {
            Ftp ftp = null;
            try {
                ftp = new Ftp(config.getServerIp(),config.getPort(),config.getUsername(),config.getPassword());
                if (ftp.existFile(remotePath+ remoteFilename)) {
                    ftp.delFile(remotePath+ remoteFilename);
                } else {
                    log.error(remotePath+ remoteFilename+ "文件不存在");
                }
            } catch (Exception e) {
                log.error("异常:{}",e.getMessage());
                throw e;
            }finally {
                if (ftp != null) {
                    try {
                        ftp.close();
                    } catch (Exception e) {
                        log.error("异常{}",e.getMessage());
                    }
                }
            }
        } else {
            try {
                SftpConfig sftpConfig = new SftpConfig(config.getServerIp(),config.getPort(),config.getUsername(),config.getPassword());
                sftpConfig.setExecType(SftpExecTypeEnum.DELETE);
                sftpConfig.setRemotePath(remotePath);
                sftpConfig.setRemoteFilename(remoteFilename);
                sftpConfig.setPrivateKey(config.getPrivateKey());
                sftpConfig.setProxyHost(config.getProxyHost());
                sftpConfig.setProxyPort(config.getProxyPort());
                sftpConfig.setProxyType(ProxyTypeEnum.getByCode(config.getProxyType()));
                SftpExecutor.execute(sftpConfig);
            } catch (Exception e) {
                log.error("下载文件失败：{}",e.getMessage());
                throw new RuntimeException("sftp 下载文件失败:"+e.getMessage());
            }
        }
    }


}
